<?php
/**
 * Created by PhpStorm.
 * User: dell1
 * Date: 2016/11/6
 * Time: 15:06
 */
header('Content-Type:text/html;charset=utf-8');
$link=mysqli_connect('localhost','root','','itcast');
if(!$link){
    die('连接数据库失败!'.mysqli_error($link));
}
mysqli_query($link,'set names utf8');
mysqli_query($link,'use `itcast`');
$city=array('北京','上海','广州','其他');
$skill=array('HTML','Javascript','PHP','C++');
$id=1;
if(!empty($_POST)){
    $fields=array('nickname','gender','email','qq','url','city','skill','description');
    foreach($fields as $v){
        $sava_data[$v]=isset($_POST[$v]) ? $_POST[$v] : '';
    }
    if($sava_data['gender']!='男' && $sava_data['gender']!='女'){
        die('保存失败：未选择性别。');
    }
    if($sava_data['city']!='未选择'&& !in_array($sava_data['city'],$city)){
        die('保存失败:您填写的城市步子啊允许的城市列表中。');
    }
    if(is_array($sava_data['skill'])){
        $sava_data['skill']=array_intersect($skill,$sava_data['skill']);
        $sava_data['skill']=implode(',',$sava_data['skill']);
    }else{
        $sava_data['skill']='';
    }
    $sql="updata `username` set";
    foreach($sava_data as $k=>$v){
        $sql.="`$k`='".mysqli_real_escape_string($link,$v)."'.";
    }
    $sql=rtrim($sql,',')."where id=$id";//rtrim($sql,',')用于去除$sql中的最后一个逗号
    $rst=mysqli_query($link,$sql);
    echo $rst ? "保存成功:$sql":"保存失败:$sql<br>".myslqi_error($link);
}
$sql="select `nickname`,`gender`,`email`,`qq`,`url`,`city`,`skill`,`description`from`userinfo` where `id`=$id";
$rst=mysqli_query($link,$sql);
$data=mysqli_fetch_assoc($rst);
$data['skill']=explode(',',$data['skill']);
require 'profile_html.php';
?>